package com.gengzp.gateway.controller;

import com.gengzp.gateway.service.GatewayService;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

/**
 * @ClassName GatewayController
 * @Description 网关控制层
 * @Author gengzp
 * @Date 2025/9/22 19:40
 */
@Controller
@RequestMapping("/gs")
public class GatewayController {

    private static final Logger logger = LoggerFactory.getLogger(GatewayController.class);

    @Autowired
    private GatewayService gatewayService;

    /**
     * 业务统一代理入口
     * <p> 所有微服务的请求都应该通过该接口进行转发 </p>
     */
    @ResponseBody
    @RequestMapping("/**")
    public Object proxy(HttpServletRequest request) {
        long startTime = System.currentTimeMillis();
        String traceId = UUID.randomUUID().toString().replace("-", "");
        logger.info("接收到请求: {}, traceId: {}", request.getRequestURI(), traceId);
        Object result = gatewayService.doExecute(request, traceId);
        logger.info("请求执行完成, 耗时: {}ms, traceId: {}\n", System.currentTimeMillis() - startTime, traceId);
        return result;
    }

}
